<template>
    <div class="category-detail">
        <a-modal width="1300px" :visible="visible" title="编辑分类" @cancel="closeModal" class="yuan-modal category-detail-modal"
            :dialog-style="{ top: '20px' }">
            <a-spin tip="Loading..." :spinning="saveLoading">
                <a-form-model layout="inline" class="yuan-form" ref="form" :model="formData" :rules="rules"
                    :class="isShow?'disable-form':''" :label-col="{ style: { width: '150px' } }"
                    :wrapper-col="{ style: { width: 'calc(100% - 160px)' } }">

                    <div class="form-panel yuan-form-col3">
                        <div class="form-panel-box">
                            <div class="form-title">分类信息</div>

                            <a-form-model-item class="yuan-form-item" label="分类名称" prop="name">
                                <a-input class="yuan-form-input" placeholder="分类名称" v-model="formData.name"></a-input>
                            </a-form-model-item>
                            <a-form-model-item class="yuan-form-item" label="排序" prop="sort">
                                <a-input type="number" class="yuan-form-input" placeholder="请输入排序"
                                    v-model="formData.sort"></a-input>
                            </a-form-model-item>
                            <a-form-model-item label="封面图" class="yuan-form-row" v-if="pageReady" prop="picture">
                                <UploadVue :uploadLength="1" @uploadChange="uploadChangeCoverImageUrl"
                                    :defImgList="formData.picture?[formData.picture]:[]" />
                                <div style="line-height:30px">jpg.png.gif格式.不超过2MB</div>
                                <div style="line-height:30px" class="c-danger">建议长宽比例为'1：1'</div>
                            </a-form-model-item>
                        </div>

                    </div>
                </a-form-model>
            </a-spin>
            <template slot="footer">
                <template v-if="isShow">
                    <a-button @click="closeModal">
                        关闭
                    </a-button>
                </template>
                <template v-else>
                    <a-button @click="modalOk" type="primary" :loading="saveLoading">
                        确认保存
                    </a-button>
                    <a-button @click="closeModal">
                        取消
                    </a-button>
                </template>

            </template>
        </a-modal>
    </div>
</template>

<script lang="ts">
import { Component, Prop, Watch, Vue } from "vue-property-decorator";
import UploadVue from "@/components/upload.vue";
import shopMallApi from "@/api/shopMallApi";
@Component({
    name: "tpl",
    components: { UploadVue },
})
export default class extends Vue {
    @Prop({ default: false }) visible!: boolean;
    @Prop({ default: false }) isShow!: boolean;
    @Prop({ default: "" }) editInfo!: any;

    @Watch("visible")
    visibleChange() {
        if (this.visible) {
            if (this.editInfo.id) {
                const picture = this.editInfo.picture || "";
                this.formData = { ...this.editInfo, picture };
                this.pageReady = true;
            } else {
                this.formData = { status: 1, parentId: 0, level: "一级", picture: "" };
                this.pageReady = true;
            }
        } else {
            this.pageReady = false;
        }
    }
    pageReady = false;
    saveLoading = false;
    formData: any = {
        pictures: "",
    };
    rules = {
        name: [{ required: true, message: "请输入分类名称", trigger: "change" }],
        sort: [{ required: true, message: "请输入排序", trigger: "change" }],
        picture: [{ required: true, message: "请上传banner图片", trigger: "change" }],
    };

    async mounted() {}
    closeModal() {
        this.$emit("closeModal");
    }

    async modalOk() {
        if (this.formData.picture === "") {
            this.$message.error("请上传封面图");
            return;
        }
        (this.$refs.form as any).validate(async (valid: boolean) => {
            if (!valid) {
                this.$message.error("必填信息不完整，请检查后重试");
                return;
            }
            this.saveLoading = true;
            try {
                const res = await shopMallApi.uploadClassify(this.formData);
                this.$message.success("保存成功");
                this.closeModal();
                this.$emit("resetList");
            } finally {
                this.saveLoading = false;
            }
        });
    }
    uploadChangeCoverImageUrl(e) {
        this.formData.picture = e.length ? e[0].url : "";
    }
}
</script>
<style lang="scss">
.category-detail-modal {
}
</style>
